using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIFriend_Application : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIFriend_Application
        {
            #region ObjectBinding Generate
            public UnityEngine.UI.ExLoopVerticalScrollRect loopVSRFriendApplication { protected set; get; }
            public UnityEngine.GameObject empty { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("loopVSRFriendApplication", out var __tbv0);
                this.loopVSRFriendApplication = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("empty", out var __tbv1);
                this.empty = __tbv1;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify



        #region fields
        private LoopScrollAdapter<UI_FriendApplicationMould> friendCells = null;


        #endregion fields

        #region properties

        protected UIB_UIFriend_Application ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;
        public List<FriendData> friendApplyList { get; set; } = new(32);
        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIFriend_Application();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.friendCells = this.InitLoopScroll<UI_FriendApplicationMould>(this.ui.loopVSRFriendApplication, this.OnFriendApplyChanged);
        }


        protected override void OnShow()
        {
            this.ui.empty.SetActive(false);
            UpdateFriendCells();
        }


        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {
            uiMsgDispatcher.AddListener(MsgEventType.FriendChanged, OnFriendChanged);
        }



        protected override void OnDispose()
        {
            this.friendApplyList = null;
        }


        #region methonds

        private void InitDataList()
        {
            this.friendApplyList.Clear();
            var dic = FriendModule.Instance.GetFriendApplyListData();

            foreach (var item in dic)
            {
                friendApplyList.Add(item.Value);
            }
        }
        private void OnFriendApplyChanged(UI_FriendApplicationMould module, int arg2)
        {
            var svData = this.friendApplyList[arg2];
            module.Show(svData);
        }
        internal void UpdateFriendCells()
        {
            this.InitDataList();
            this.friendCells.RefillCellsAndSelected(friendApplyList.Count, 0);
            if (friendApplyList.Count == 0) this.ui.empty.SetActive(true);
        }
        private void OnFriendChanged()
        {
            if (isActive) UpdateFriendCells();
        }


        #endregion methonds
    }
}